package com.insigma.business.qrcode.placement.dto.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.insigma.business.common.utils.ToolsHelper;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author max
 * @Description:
 * @date 2024-04-23 16:29
 */
@Getter
public class ResetSubsidyDistrListener extends AnalysisEventListener<ResetSubsidyDistrInOutDTO> {

    private List<ResetSubsidyDistrInOutDTO> validateNotPassList = new ArrayList<>();

    private List<ResetSubsidyDistrInOutDTO> validatePassList = new ArrayList<>();

    @Override
    public void invoke(ResetSubsidyDistrInOutDTO importDTO, AnalysisContext analysisContext) {
        if(ToolsHelper.isEmpty(importDTO)){
            return;
        }
        StringBuilder failRes = new StringBuilder();
        if(ToolsHelper.isEmpty(importDTO.getPsnName())){
            failRes.append("||姓名为空;");
        }
        if(ToolsHelper.isEmpty(importDTO.getIdCardNum())){
            failRes.append("||证件号码为空;");
        }
        if(importDTO.getIdCardNum().length() != 18 && importDTO.getIdCardNum().length() != 15 ){
            failRes.append("||证件号码有误;");
        }
        if(ToolsHelper.isEmpty(importDTO.getAdmdvs())){
            failRes.append("||安置地为空;");
        }

        Pattern pattern = Pattern.compile("^\\d+$");// 匹配整数
        if(ToolsHelper.isEmpty(importDTO.getPayYear())){
            failRes.append("||年份为空;");
        }
        if(!pattern.matcher(importDTO.getPayYear()).matches() || importDTO.getPayYear().length() != 4){
            failRes.append("||年份输入有误;");
        }
        String payMonth = importDTO.getPayMonth();
        if(ToolsHelper.isEmpty(payMonth)){
            failRes.append("||月份为空;");
        }
        if(!pattern.matcher(payMonth).matches()
                || Integer.parseInt(payMonth) == 0 || Integer.parseInt(payMonth) > 12){
            failRes.append("||月份输入有误;");
        }
        if(payMonth.length() == 1 && Integer.parseInt(payMonth) != 0){
            importDTO.setPayMonth("0" + payMonth);
        }

        if(ToolsHelper.isEmpty(importDTO.getBankAcctName())){
            failRes.append("||银行账户名称为空;");
        }
        if(ToolsHelper.isEmpty(importDTO.getBankAcctName())){
            failRes.append("||银行账号为空;");
        }
        if(ToolsHelper.isEmpty(importDTO.getBankAddr())){
            failRes.append("||开户行地址为空;");
        }
        if(ToolsHelper.isEmpty(importDTO.getEndinsuClctstd())){
            failRes.append("||养老参保基数为空;");
        }
        if(ToolsHelper.isEmpty(importDTO.getMedinsuClctstd())){
            failRes.append("||医保参保基数为空;");
        }
        if(ToolsHelper.isEmpty(importDTO.getPayAmt()) || BigDecimal.ZERO.compareTo(importDTO.getPayAmt()) == 0){
            failRes.append("||发放金额为空;");
        }

        if(failRes.length() == 0){
            validatePassList.add(importDTO);
        }else {
            importDTO.setMemo((ToolsHelper.isEmpty(importDTO.getMemo()) ? "" : importDTO.getMemo()) + failRes.toString());
            validateNotPassList.add(importDTO);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }
}
